﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using GBG_DBM.UI;
using GBG_DBM.GlobalParameters;
using GBG_DBM.Support.DAL;
using GBG_DBM.Support.BLL;

namespace GBG_DBM.UI
{
    public partial class Setting_UI : Form
    {
        #region FILEDS

        private OleDbConnection _dbConnection = new OleDbConnection();
        private OleDbCommand _cmd = new OleDbCommand();
        private string _connectionString;
        private string _defBackupPath = GlobalParams.DefaultBackupPath().ToString();
        private ConfOperator _iniOperator = new ConfOperator(GlobalParams.INIFilePath().ToString());

        #endregion

        #region PROPERTIES
        #endregion

        #region CONSTRUCTORS

        public Setting_UI()
        {
            InitializeComponent();

            Backup_Path_TextBox_Setting_UI.Text = _defBackupPath;
            //disable customize backup directory function
            this.Backup_Path_TextBox_Setting_UI.Enabled = false;
            this.Select_Backup_Path_Setting_UI.Enabled = false;

            //this.AcceptButton = OK_Setting_UI;
            //this.CancelButton = Cancel_Setting_UI;
        }

        #endregion

        #region METHODS

        private void Cancel_Setting_UI_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Select_Backup_Path_Setting_UI_Click(object sender, EventArgs e)
        {
            //BackupPathDialog.ShowDialog();
            if (BackupPathDialog.ShowDialog() == DialogResult.OK)
            {
                Backup_Path_TextBox_Setting_UI.Text = BackupPathDialog.SelectedPath;
            }
        }

        private void OK_Setting_UI_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Reset_DB_Password_Setting_UI_Click(object sender, EventArgs e)
        {
            //this program logic can be optimized
            if (New_PW_Setting_UI.Text == New_PW2_Setting_UI.Text)
            {
                string _finalPW = New_PW2_Setting_UI.Text;

                if (_finalPW == DBHelper.CurrentPassword)
                {
                    MessageBox.Show("Please choose a password you haven't previously used.", "Information",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1);
                    New_PW_Setting_UI.Text = "";
                    New_PW2_Setting_UI.Text = "";
                    New_PW_Setting_UI.Focus();
                }
                else if (_finalPW == null || _finalPW == "")
                {
                    MessageBox.Show("Please choose a password for the Database.", "Information",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1);
                    New_PW_Setting_UI.Text = "";
                    New_PW2_Setting_UI.Text = "";
                    New_PW_Setting_UI.Focus();
                }
                else
                {
                    //to change access database password, we need to set the connection as exclusive connection
                    _connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + GlobalParams.DBPath + ";Mode=Share Exclusive;Jet OLeDb:DataBase Password=" + DBHelper.CurrentPassword;
                    _dbConnection.ConnectionString = _connectionString;
                    try
                    {
                        _dbConnection.Open();
                        _cmd.Connection = _dbConnection;
                        _cmd.CommandText = "ALTER DATABASE PASSWORD " + "[" + _finalPW + "]" + " [" + DBHelper.CurrentPassword + "]";
                        _cmd.ExecuteNonQuery();
                        DBHelper.SetPassword(_finalPW);
                        _dbConnection.Close();       
                    }
                    catch (OleDbException _OleErr)
                    {
                        throw new Exception(_OleErr.Message);
                    }
                    finally
                    {
                        _dbConnection.Close();

                        //update global connectionstring
                        GBG_DBM.Properties.Settings.Default.DatabaseConnectionString = DBHelper.ConnectionString;
                    }

                    MessageBox.Show("Password updated successfully!", "Information",
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Information,
                               MessageBoxDefaultButton.Button1);
                    DBHelper.SetPassword(_finalPW);
                    //New_PW_Setting_UI.Text = "";
                    //New_PW2_Setting_UI.Text = "";
                    New_PW_Setting_UI.Enabled = false;
                    New_PW2_Setting_UI.Enabled = false;
                    Reset_DB_Password_Setting_UI.Enabled = false;
                }
            }
            else
            {
                MessageBox.Show("Please enter the same Password in both Password fields.", "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1);

                New_PW_Setting_UI.Text = "";
                New_PW2_Setting_UI.Text = "";
                New_PW_Setting_UI.Focus();
            }
        }

        #endregion
    }
}
